package com.marlowe.service.book;

import com.marlowe.api.constants.ResultDTO;
import com.marlowe.api.enums.HttpCode;
import com.marlowe.api.model.book.BookClassDTO;
import com.marlowe.api.service.book.BookClassService;
import com.marlowe.dao.mapper.book.BookClassMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @program: lib-web
 * @description: 书籍分类Service实现类
 * @author: Marlowe
 * @create: 2020-12-28 01:18
 **/
@Service
public class BookClassServiceImpl implements BookClassService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BookClassMapper bookClassMapper;


    @Override
    public ResultDTO findListByName(String name) {

        // 非空判断
        if (StringUtils.isEmpty(name)) {
            return new ResultDTO(HttpCode.FAIL.getCode(), "搜索关键字不能为空");
        }
        // 业务查找
        List<BookClassDTO> list = bookClassMapper.findListByName(name);
        if (CollectionUtils.isEmpty(list)) {
            return new ResultDTO(HttpCode.FAIL.getCode(), "暂无对应的分类数据");
        }

        return new ResultDTO(HttpCode.SUCCESS.getCode(), "查询成功", list);
    }

    @Override
    public ResultDTO findById(int id) {
        if (0 == id) {
            return new ResultDTO(HttpCode.FAIL.getCode(), "数据id不能为空");
        }
        BookClassDTO bookClassDTO = bookClassMapper.findById(id);
        if (null == bookClassDTO) {
            return new ResultDTO(HttpCode.FAIL.getCode(), "暂无分类数据");
        }
        return new ResultDTO(HttpCode.SUCCESS.getCode(), "查询成功", bookClassDTO);
    }

    @Override
    public ResultDTO insert(BookClassDTO entity) {
        if (StringUtils.isEmpty(entity.getName())) {
            return new ResultDTO(HttpCode.FAIL.getCode(), "分类名称不能为空");
        }
        int insert = bookClassMapper.insert(entity);
        if (insert <= 0) {
            return new ResultDTO(HttpCode.FAIL.getCode(), "新增数据失败");
        }
        return new ResultDTO(HttpCode.SUCCESS.getCode(), "新增成功");
    }

    @Override
    public ResultDTO update(BookClassDTO entity) {
        if (StringUtils.isEmpty(entity.getId())) {
            return new ResultDTO(HttpCode.FAIL.getCode(), "数据ID不能为空");
        }
        int insert = bookClassMapper.insert(entity);
        if (insert <= 0) {
            return new ResultDTO(HttpCode.FAIL.getCode(), "更新失败");
        }
        return new ResultDTO(HttpCode.SUCCESS.getCode(), "更新成功");
    }

    @Override
    public ResultDTO delete(int id) {
        if (0 == id) {
            return new ResultDTO(HttpCode.FAIL.getCode(), "数据ID不能为空");
        }
        int delete = bookClassMapper.delete(id);
        if (delete <= 0) {
            return new ResultDTO(HttpCode.FAIL.getCode(), "删除失败");
        }
        return new ResultDTO(HttpCode.SUCCESS.getCode(), "删除成功");
    }
}
